package com.yanqu.road.dao.impl.wingroom.chakra;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.wingroom.chakra.data.*;
import com.yanqu.road.utils.FixSizeLinkedList;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class WingRoomChakraBeVisitRecordDaoImpl extends TempDataObjectDao<UserWingRoomChakraBeVisitRecord> {

    private static String insertSql = "INSERT INTO `t_u_wingroom_chakra_be_visit_record`(`userId`, `visitUserId`, `visitUserInfo`, `createTime`) VALUES (?, ?, ?, ?)";
    @Override
    protected boolean addData(UserWingRoomChakraBeVisitRecord data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVisitUserId()));
        String userInfoJson = "";
        if (data.getVisitUserInfo() != null) {
            userInfoJson = JSONObject.toJSONString(data.getVisitUserInfo());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, userInfoJson));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserWingRoomChakraBeVisitRecord data) {
        return false;
    }


    @Override
    protected UserWingRoomChakraBeVisitRecord parseData(ResultSet rs) throws SQLException {
        UserWingRoomChakraBeVisitRecord data = new UserWingRoomChakraBeVisitRecord();
        data.setUserId(rs.getLong("userId"));
        data.setVisitUserId(rs.getLong("visitUserId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("visitUserInfo"))) {
            UserBaseInfo userBaseInfo = JSONObject.parseObject(rs.getString("visitUserInfo"), UserBaseInfo.class);
            data.setVisitUserInfo(userBaseInfo);
        }
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }

    /**
     * 获取数据
     * @param userId
     * @return
     */
    public void initData(long userId, FixSizeLinkedList<UserWingRoomChakraBeVisitRecord> userBeVisitRecordList) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_wingroom_chakra_be_visit_record where userId = ? order by createTime desc limit " + userBeVisitRecordList.getCapacity();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWingRoomChakraBeVisitRecord data = parse(rs);
                userBeVisitRecordList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }
}
